package com.ellecity06.common.helper

import android.app.Activity
import java.util.*

/**
 * @author zavier
 * @time 2023/4/15 9:57
 * @des Activity管理栈 保存activity的实例，当程序退出时情况activity
 */
class ActivityManagerHelper {

    /**
     * 弹出Activity
     *
     * @param activity
     * @param isFinish 是否手动finish掉activity
     * @see [类、类.方法、类.成员]
     */
    fun popActivity(activity: Activity?, isFinish: Boolean = false) {
        if (activity != null) {
            if (isFinish) {
                activity.finish()
            }
            activityList!!.remove(activity)
        }
    }

    /**
     * 获取栈顶的Activity
     *
     * @return
     * @see [类、类.方法、类.成员]
     */
    fun currentActivity(): Activity? {
        return if (!activityList!!.isEmpty()) {
            // 该方法Returns the last element in this vector.也就是说栈顶的
            activityList!!.lastElement() as Activity
        } else {
            null
        }
    }

    /**
     * 获取栈的Acitivity
     *
     * @param dept 索引（倒叙形式）
     * @return
     * @see [类、类.方法、类.成员]
     */
    fun getNextActivity(dept: Int): Activity? {
        // 反序返回指定索引号的Activity实例，但不做弹出栈操作
        return if (dept <= activityList!!.size) {
            activityList!![dept]
        } else {
            null
        }
    }

    /**
     * Activity入栈
     *
     * @param activity
     * @see [类、类.方法、类.成员]
     */
    fun addActivity(activity: Activity) {
        if (activityList == null) {
            activityList = Stack<Activity>()
        }
        activityList!!.add(activity)
    }

    /**
     * 清空Activity栈
     *
     * @see [类、类.方法、类.成员]
     */
    fun removeAllActivity() {
        while (activityList != null && activityList!!.size > 0) {
            val activity = currentActivity()
            if (activity != null) {
                popActivity(activity, true)
            }
        }
    }

    fun finshAll() {
        activityList?.clear()

    }

    /**
     * 获取指定的Acitivity
     *
     * @param （倒叙形式）
     * @return
     * @see [类、类.方法、类.成员]
     */
    val foundCampaignDetailActivity: Activity?
        get() {
            val activity: Activity? = null
            if (!activityList!!.isEmpty() && activityList!!.size > 0) {
                for (i in activityList!!.indices) {

                }
            }
            return activity
        }

    private object SingletonHolder {
        val instance = ActivityManagerHelper()
    }

    companion object {
        /**
         * 获取管理栈集合
         */
        /**
         * 栈对象,用于存放Activity对象，以便于管理
         */
        private var activityList: Stack<Activity>? = null

        val instance: ActivityManagerHelper = SingletonHolder.instance
    }
}